package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.HandleaffairsServiceSecondaryActivity;
import com.ln.lnzw.adapter.ImplementationSubjectAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.bean.NewImplementationSubjectBean;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.net.HttpMethodString;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/16.
 * 实施主体
 */

public class ImplementationSubjectFragment extends BaseFragment {

    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    Unbinder unbinder;
    private String url;
    private String name;
    private ImplementationSubjectAdapter mAdapter;
    private List<NewImplementationSubjectBean.ResultBean> mList = new ArrayList<>();
//    public static String  IMPLEMENTATION = "Implementation";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.implementationsubject_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setView();

        initData();
    }
    private void setView() {
//        url = getArguments().getString("url");
        name = getArguments().getString("name");
        rvRecyclerview.setNestedScrollingEnabled(false);
        rvRecyclerview.setLayoutManager(new GridLayoutManager(activity, 2));
        mAdapter = new ImplementationSubjectAdapter(R.layout.item_implementation_subject, mList);
        rvRecyclerview.setAdapter(mAdapter);

        rvRecyclerview.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle b = new Bundle();
                b.putString("cDesc",mList.get(position).getGroupId());
                ActivityUtils.startActivity(b,activity,HandleaffairsServiceSecondaryActivity.class);
            }
        });
//        //屏蔽上拉加载
//        refreshLayout.setEnableLoadMore(false);
//        //设置下拉刷新监听事件
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
//                new Timer().schedule(new TimerTask() {
//                    @Override
//                    public void run() {
//                        //结束加载
//                        refreshLayout.finishRefresh();
////                        //加载失败的话3秒后结束加载
////                        refreshLayout.finishRefresh(3000);
//                    }
//                }, 3000);
//            }
//        });

    }
    private void initData() {
//        Log.i("666", "initData: "+url);
//        HttpMethodString.getInstance().info.getStringData(url)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
                        Gson gson = new Gson();
                            NewImplementationSubjectBean newImplementationSubjectBean = gson.fromJson(activity.spUtils.getString(AppConstant.IMPLEMENTATION),NewImplementationSubjectBean.class);
                        if (mList != null || !mList.isEmpty()) {
                            mList.clear();
                        }
                        mList.addAll(newImplementationSubjectBean.getResult());
//                        activity.spUtils.put(AppConstant.IMPLEMENTATION,s);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        Log.i("666", "onError: "+e.toString());
//                    }
//
//                    @Override
//                    public void onComplete() {
                    mAdapter.notifyDataSetChanged();

//                    }
//                });
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
